package com.mz.mattress.client.service.impl;

import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Card;
import com.mz.mattress.api.entity.CardMerchant;
import com.mz.mattress.api.entity.Member;
import com.mz.mattress.api.entity.MemberCard;
import com.mz.mattress.client.mapper.CardMapper;
import com.mz.mattress.client.mapper.CardMerchantMapper;
import com.mz.mattress.client.mapper.MemberCardMapper;
import com.mz.mattress.client.mapper.MemberMapper;
import com.mz.mattress.client.service.CardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@Service
public class CardServiceImpl implements CardService {

    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private CardMerchantMapper cardMerchantMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberCardMapper memberCardMapper;

    @Override
    public Result<List<Card>> list(Long memberId) {

        Member member = memberMapper.selectById(memberId);

        if (member == null ||
                member.getLastMerchantId() == null) {
            return Result.ok();
        }

        List<Card> list = cardMapper.selectByMerchantId(member.getLastMerchantId());
        return Result.ok(list);

    }

    @Override
    public Result<Card> getById(Integer id) {

        Card card = cardMapper.selectById(id);
        if (card == null) {
            return Result.fail("找不到广告");
        }

        List<CardMerchant> list = cardMerchantMapper.selectByCardId(id);
        card.setMerchantList(list);
        return Result.ok(card);
    }

    @Override
    public Result sendCard(Long memberId, String orderId) {

        Member member = memberMapper.selectById(memberId);
        if (member == null || member.getLastMerchantId() == null) {
            return Result.ok();
        }

        //商家可用的卡项
        List<Card> list = cardMapper.selectByMerchantId(member.getLastMerchantId());

        for (Card card : list) {

            LocalDate endDate = LocalDate.parse(card.getEndDate());
            if (endDate.isBefore(LocalDate.now())) {
                log.info("card_is_expired,cardId=" + card.getId());
                continue;
            }

            //领取规则 0无限制 1仅领取一次 2每月一次
            if (checkCanSend(memberId, card)) {
                doSendCard(memberId, member.getMobile(), card, Long.valueOf(orderId));
            }

        }

        return Result.ok();
    }

    private boolean checkCanSend(Long memberId, Card card) {

        //领取规则 0无限制 1仅领取一次 2每月一次
        if (card.getSendRule() == 0) {
            return true;
        } else if (card.getSendRule() == 1) {

            Integer count = memberCardMapper.selectCountByMemberId(memberId, card.getId());
            return count == 0;

        } else if (card.getSendRule() == 2) {
            //2每月一次 java中，获取本月第一天时间
            LocalDate localDate = LocalDate.now().withDayOfMonth(1);
            String month = localDate.toString();
            Integer count = memberCardMapper.selectCountMonthByMemberId(memberId, card.getId(), month);
            return count == 0;
        }

        return false;
    }

    private void doSendCard(Long memberId, String mobile, Card card, Long orderId) {

        MemberCard memberCard = new MemberCard();
        memberCard.setMemberId(memberId);
        memberCard.setMobile(mobile);
        memberCard.setCardId(card.getId());
        memberCard.setMerchantId(card.getMerchantId());
        memberCard.setMerchantName(card.getMerchantName());
        memberCard.setProvince(card.getProvince());
        memberCard.setCity(card.getCity());
        memberCard.setName(card.getName());
        memberCard.setType(card.getType());
        memberCard.setMoney(card.getMoney());
        memberCard.setEndTime(card.getEndDate());
        memberCard.setOrderId(orderId);

        LocalDate startDate = LocalDate.parse(card.getEndDate());
        if (startDate.isBefore(LocalDate.now())) {
            //在时间内 就启动
            memberCard.setStatus(1);
        } else {
            memberCard.setStatus(0);
        }

        memberCard.setSendType(2);
        memberCardMapper.insert(memberCard);
    }


    public static void main(String[] args) {

        LocalDate localDate = LocalDate.now().withDayOfMonth(1);
        System.out.println(localDate.toString());

        LocalDate endDate = LocalDate.parse("2025-10-30");

        System.out.println(endDate.isBefore(LocalDate.now()));

    }

}
